package com.xysq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CouponBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.adapter.CouponAdapterForCouponBO;
import com.xysq.lemon.R;
import com.xysq.widget.LoadData;
import com.xysq.widget.ScrollListener;

import java.util.List;

import butterknife.InjectView;

public class SortCouponModelActivity extends BaseActivity implements View.OnClickListener {

    public static final String INDUSTRY_ID = "industry_id";
    public static final String INDUSTRY_NAME = "industry_name";
    private int industryId;
    private String title;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.list_coupon)
    ListView couponList;
    @InjectView(R.id.txt_null_data)
    TextView nullDataTxt;
    @InjectView(R.id.txt_title)
    TextView titleTxt;
    CouponAdapterForCouponBO couponAdapterForCouponBO;
    ScrollListener scrollListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_coupon_model);
        getExtra();
        initView();
        loadData();
    }

    private void loadData() {
        appAction.listCouponModel("", 0, 0, industryId, scrollListener.getCurrentPage(), PAGE_SIZE, 0, new CallbackListener<List<CouponBO>>() {
            @Override
            public void onSuccess(List<CouponBO> data) {
                Log.e("++++获取券成功", data.size() + "");
                if(scrollListener.getCurrentPage() == 1){
                    if(data.size() > 0){
                        couponAdapterForCouponBO.clearItems();
                        couponAdapterForCouponBO.setItems(data);
                    }else {
                        nullDataTxt.setVisibility(View.VISIBLE);
                    }
                }else {
                    if(data.size() > 0){
                        nullDataTxt.setVisibility(View.INVISIBLE);
                        couponAdapterForCouponBO.addItems(data);
                        scrollListener.setLoadFinish(true);
                    }
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                scrollListener.setLoadFinish(true);
                Log.e("++++获取券失败", message);
            }
        });
    }

    private void initView() {
        scrollListener = new ScrollListener(couponList, true, new LoadData() {
            @Override
            public void loadMyData() {
                loadData();
            }
        });
        couponList.setOnScrollListener(scrollListener);
        backIbtn.setOnClickListener(this);
        couponAdapterForCouponBO = new CouponAdapterForCouponBO(this);
        couponList.setAdapter(couponAdapterForCouponBO);
        couponList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CouponBO couponBO = (CouponBO) couponAdapterForCouponBO.getItem(i);
                Intent intent = new Intent(SortCouponModelActivity.this, CommodityDetailsActivity.class);
                intent.putExtra(CommodityDetailsActivity.COUPONMODEL_ID, couponBO.getId());
                startActivity(intent);
            }
        });
    }

    private void getExtra() {
        Intent intent = getIntent();
        industryId = intent.getIntExtra(INDUSTRY_ID, -1);
        title = intent.getStringExtra(INDUSTRY_NAME);
        titleTxt.setText(title);
        Log.e("industryId", industryId + "");
    }

    @Override
    public void onClick(View view) {
        if (view == backIbtn) {
            finish();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("商城分类页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("商城分类页");
        MobclickAgent.onPause(this);
    }
}
